class SingleLinkedList {
    private int usedSize;
    static class LinkedNode{
        public int val;
        public LinkedNode nextNode;
        public LinkedNode(){

        }
        public LinkedNode(int val) {
            this.val = val;
        }
    }

    public LinkedNode heapNode;
    public SingleLinkedList() {
        this.heapNode = new LinkedNode(); // 哨兵节点
    }

    //头插法
    public void addFirst(int data){
        LinkedNode heap = new LinkedNode(data);
        if (this.usedSize == 0){
            this.heapNode.nextNode = heap;
            this.usedSize++;
            return;
        }
        LinkedNode cur = this.heapNode.nextNode;
        this.heapNode.nextNode = heap;
        heap.nextNode = cur;
        this.usedSize++;
    }
    //尾插法
    public void addLast(int data){
        LinkedNode cur = this.heapNode.nextNode;
        if (cur == null) {
            addFirst(data);
        } else {
            while (cur.nextNode != null){
                cur = cur.nextNode;
            }
            LinkedNode last = new LinkedNode(data);
            cur.nextNode = last;
        }
        this.usedSize++;
    }
    private boolean isLegal(int index){
        return index >= 0 && index <= this.usedSize;
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data){
        if (!isLegal(index)){
            System.out.println("下标不合法");
            return false;
        }
        if (index == 0){
            addFirst(data);
            return true;
        }
        LinkedNode cur = this.heapNode.nextNode;
        LinkedNode node = new LinkedNode(data);
        while (index > 1){
            cur = cur.nextNode;
            index--;
        }
        LinkedNode t = cur.nextNode;
        cur.nextNode = node;
        node.nextNode = t;
        this.usedSize++;
        return true;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        LinkedNode cur = this.heapNode.nextNode;
        while (cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.nextNode;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        LinkedNode cur = this.heapNode.nextNode;
        if (cur.val == key){
            this.heapNode.nextNode = cur.nextNode;
            cur.nextNode = null;
            this.usedSize--;
            return;
        }
        while (cur.nextNode != null){
            if (cur.nextNode.val == key){
                LinkedNode t = cur.nextNode;
                cur.nextNode = cur.nextNode.nextNode;
                t.nextNode = null;
                this.usedSize--;
                return;
            }
            cur = cur.nextNode;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        LinkedNode cur = this.heapNode.nextNode;
        while (cur != null && cur.nextNode != null){
            if (cur.nextNode.val == key){
                LinkedNode t = cur.nextNode.nextNode;
                cur.nextNode.nextNode = null;
                cur.nextNode = t;
                this.usedSize--;
            } else {
                cur = cur.nextNode;
            }
        }
        if (this.heapNode.nextNode != null && this.heapNode.nextNode.val == key){
            LinkedNode t = this.heapNode.nextNode.nextNode;
            this.heapNode.nextNode.nextNode = null;
            this.heapNode.nextNode = t;
            this.usedSize--;
        }
    }
    //得到单链表的长度
    public int size(){
        return this.usedSize;
    }
    public boolean isEmpty(){
        return this.usedSize == 0;
    }
    public void display(){
        LinkedNode cur = this.heapNode.nextNode;
        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.nextNode;
        }
        System.out.println();
    }
    public void clear(){
        LinkedNode cur = this.heapNode.nextNode;
        this.heapNode.nextNode = null;
        LinkedNode t;
        while (cur != null){
            t = cur.nextNode;
            cur.nextNode = null;
            cur = t;
        }
        this.usedSize = 0;
    }
}